import * as api from "../api/api";
import request from "../utils/request";
import { setUserInfo, pageSize } from "../utils/common";

export default {
  namespace: "store",
  state: {
    store: {},
    stores: { totalCount: 0, items: [] },
    storeSearchs: { pageIndex: 1, pageSize, keywords: "" },
    storeVerifySearchs: { pageIndex: 1, pageSize, keywords: "", status: "0" },
    businessScope: {},
    businessScopes: { totalCount: 0, items: [] },
    businessScopeSearchs: { pageIndex: 1, pageSize: pageSize },
    storeBalance: {},
    storeBalances: { totalCount: 0, items: [] },
    storeBalanceSearchs: { pageIndex: 1, pageSize, keywords: "" },
    storeRegister: {},
    storeRegisters: { totalCount: 0, items: [] },
    storeRegisterSearchs: { pageIndex: 1, pageSize }
  },
  effects: {
    /**
     * 获取店铺列表
     * @param param0
     * @param param1
     */
    *getStores({ payload }, { call, put }) {
      const { keywords, isInValid, pageIndex, pageSize } = payload;
      const formKeywords = keywords,
        formIsInValid = isInValid,
        formSkipCount = (pageIndex - 1) * pageSize,
        formMaxResultCount = pageSize;
      const stores: api.AbpApplicationServicesDtoPagedResultDtoSingleUserMallStoresVOStoreVO = yield request(
        api.ManagestoreManageApiFetchParamCreactor.managestoreManageGetStoresBySearchAsync(
          {
            formKeywords,
            formIsInValid,
            formSkipCount,
            formMaxResultCount
          }
        )
      );
      yield put({
        type: "save",
        payload: { stores, storeSearchs: { keywords, pageIndex, pageSize } }
      });
    },
    /**
     * 获取店铺结算申请列表
     * @param param0
     * @param param1
     */
    *getBalanceStores({ payload }, { call, put }) {
      const { keywords, status, pageIndex, pageSize } = payload;
      const formKeywords = keywords,
        formStatus = status,
        formSkipCount = (pageIndex - 1) * pageSize,
        formMaxResultCount = pageSize;
      const storeBalances: api.AbpApplicationServicesDtoPagedResultDtoSingleUserMallStoresVOStoreBalanceVO = yield request(
        api.ManagestoreManageApiFetchParamCreactor.managestoreManageGetVerifyStoreBalancesByAsync(
          {
            formKeywords,
            formStatus,
            formSkipCount,
            formMaxResultCount
          }
        )
      );
      yield put({
        type: "save",
        payload: {
          storeBalances,
          storeBalanceSearchs: { keywords, pageIndex, pageSize, status }
        }
      });
    },
    /**
     * 获取店铺申请项目列表
     * @param param0
     * @param param1
     */
    *getStoreRegisters({ payload }, { call, put }) {
      const { pageIndex, pageSize } = payload;
      const formSkipCount = (pageIndex - 1) * pageSize,
        formMaxResultCount = pageSize;
      const storeRegisters: api.AbpApplicationServicesDtoPagedResultDtoSingleUserMallStoresVOStoreRegisterVO = yield request(
        api.ManagestoreManageApiFetchParamCreactor.managestoreManageGetStoreRegisterInfo(
          {
            formSkipCount,
            formMaxResultCount
          }
        )
      );
      yield put({
        type: "save",
        payload: {
          storeRegisters,
          storeRegisterSearchs: { pageIndex, pageSize }
        }
      });
    },
    /**
     * 获取店铺结算申请详情
     * @param param0
     * @param param1
     */
    *getBalanceStore({ payload }, { call, put }) {
      const formId = payload.id;
      const storeBalance: api.SingleUserMallStoresVOStoreBalanceVO = yield request(
        api.ManagestoreManageApiFetchParamCreactor.managestoreManageGetVerifyStoreBalanceByAsync(
          {
            formId
          }
        )
      );
      yield put({ type: "save", payload: { storeBalance } });
    },

    /**
     * 根据用户Id获取店铺
     * @param param0
     * @param param1
     */
    *getStoreByUserId({}, { call, put }) {
      const store: api.SingleUserMallStoresVOStoreVO = yield request(
        api.ManagestoreManageApiFetchParamCreactor.managestoreManageGetStoreByUserId(
          {}
        )
      );
      yield put({ type: "save", payload: { store } });
    },

    /**
     * 获取待审核店铺列表
     * @param param0
     * @param param1
     */
    *getVerifyStores({ payload }, { call, put }) {
      const { keywords, status, pageIndex, pageSize } = payload;
      const formKeywords = keywords,
        formStatus = status,
        formSkipCount = (pageIndex - 1) * pageSize,
        formMaxResultCount = pageSize;
      const stores: api.AbpApplicationServicesDtoPagedResultDtoSingleUserMallStoresVOStoreVO = yield request(
        api.ManagestoreManageApiFetchParamCreactor.managestoreManageGetVerifyStoresByAsync(
          {
            formKeywords,
            formStatus,
            formSkipCount,
            formMaxResultCount
          }
        )
      );
      yield put({
        type: "save",
        payload: {
          stores,
          storeVerifySearchs: { keywords, pageIndex, pageSize, status }
        }
      });
    },
    /**
     * 获取店铺详情
     * @param param0
     * @param param1
     */
    *getStore({ payload }, { call, put }) {
      const formId = payload.id;
      const store: api.SingleUserMallStoresVOStoreVO = yield request(
        api.ManagestoreManageApiFetchParamCreactor.managestoreManageGetStoreAsync(
          {
            formId
          }
        )
      );
      yield put({ type: "save", payload: { store } });
    },
    /**
     * 保存店铺
     * @param param0
     * @param param1
     */
    *saveStore({ payload }, { call, put, select }) {
      const { form, callback } = payload;
      const { storeSearchs } = yield select(({ store }) => store);
      yield request(
        api.ManagestoreManageApiFetchParamCreactor.managestoreManageSaveStoreAsync(
          { form }
        )
      );
      yield put({ type: "getStores", payload: { ...storeSearchs } });
      callback && callback(true);
    },
     /**
     * 保存店铺申请项目
     * @param param0
     * @param param1
     */
    *saveStoreRegister({ payload }, { call, put, select }) {
      const { form, callback } = payload;
      const { storeRegisterSearchs } = yield select(({ store }) => store);
      yield request(
        api.ManagestoreManageApiFetchParamCreactor.managestoreManageSaveStoreRegisterAsync(
          { form }
        )
      );
      yield put({ type: "getStoreRegisters", payload: { ...storeRegisterSearchs } });
      callback && callback(true);
    },
    /**
     * 入驻商家修改店铺
     * @param param0
     * @param param1
     */
    *saveStoreByUser({ payload }, { call, put, select }) {
      const { form, callback } = payload;
      const { storeSearchs } = yield select(({ store }) => store);
      yield request(
        api.ManagestoreManageApiFetchParamCreactor.managestoreManageSaveStoreByUserAsync(
          { form }
        )
      );
      yield put({ type: "getStoreByUserId" });
      callback && callback(true);
    },
    /**
     * 店铺审核
     * @param param0
     * @param param1
     */
    *verifyStore({ payload }, { call, put, select }) {
      const { form, callback } = payload;
      const { storeSearchs } = yield select(({ store }) => store);
      yield request(
        api.ManagestoreManageApiFetchParamCreactor.managestoreManageVerifyStoresAsync(
          { form }
        )
      );
      yield put({ type: "getVerifyStores", payload: { ...storeSearchs } });
      callback && callback(true);
    },
    /**
     * 查询经营范围
     * @param param0
     * @param param1
     */
    *getBusinessScopes({ payload }, { call, put }) {
      const { pageIndex, pageSize } = payload;
      const formSkipCount = (pageIndex - 1) * pageSize,
        formMaxResultCount = pageSize;
      const businessScopes = yield request(
        api.ManageitemManageApiFetchParamCreactor.manageitemManageGetAllBusinessScopeAsync(
          { formSkipCount, formMaxResultCount }
        )
      );
      yield put({
        type: "save",
        payload: {
          businessScopes,
          businessScopeSearchs: { pageIndex, pageSize }
        }
      });
    },
    /**
     * 结算店铺
     * @param param0
     * @param param1
     */
    *saveBanlanceStore({ payload }, { call, put, select }) {
      const { form, callback } = payload;
      const { storeSearchs } = yield select(({ store }) => store);
      yield request(
        api.ManagestoreManageApiFetchParamCreactor.managestoreManageSetStoreBalanceAsync(
          { form }
        )
      );
      yield put({ type: "getStores", payload: { ...storeSearchs } });
      callback && callback(true);
    },
    /**
     * 删除店铺
     * @param param0
     * @param param1
     */
    *remove({ payload }, { call, put, select }) {
      const { form, callback } = payload;
      const { storeSearchs } = yield select(({ store }) => store);
      yield request(
        api.ManagestoreManageApiFetchParamCreactor.managestoreManageRemoveStoreAsync(
          { form }
        )
      );
      yield put({ type: "getStores", payload: { ...storeSearchs } });
      callback && callback(true);
    },
    /**
     * 提交结算申请
     * @param param0
     * @param param1
     */
    *pushBanlanceStore({ payload }, { call, put }) {
      const { form, callback } = payload;
      yield request(
        api.ManagestoreManageApiFetchParamCreactor.managestoreManagePushStoreBalanceAsync(
          { form }
        )
      );
      yield put({ type: "getStoreByUserId" });
      callback && callback(true);
    },
    /**
     * 店铺结算审核
     * @param param0
     * @param param1
     */
    *verifyBalanceStore({ payload }, { call, put, select }) {
      const { form, callback } = payload;
      const { storeBalanceSearchs } = yield select(({ store }) => store);
      yield request(
        api.ManagestoreManageApiFetchParamCreactor.managestoreManageVerifyStoreBalanceAsync(
          { form }
        )
      );
      yield put({
        type: "getBalanceStores",
        payload: { ...storeBalanceSearchs }
      });
      callback && callback(true);
    }
  },
  reducers: {
    save(state, action) {
      return { ...state, ...action.payload };
    }
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname, search }) => {
        switch (pathname) {
          case "/store/stores":
            dispatch({
              type: "getStores",
              payload: { pageIndex: 1, pageSize, isInValid: `false` }
            });
            dispatch({
              type: "getBusinessScopes",
              payload: { keywords: "", pageIndex: 1, pageSize }
            });
            break;
          case "/store/verifystores":
            dispatch({
              type: "getVerifyStores",
              payload: { pageIndex: 1, pageSize, status: "0" }
            });
            dispatch({
              type: "getBusinessScopes",
              payload: { keywords: "", pageIndex: 1, pageSize }
            });
            break;
          case "/store/storeinfo":
            dispatch({ type: "getStoreByUserId", payload: {} });
            break;
          case "/store/verifybalancestores":
            dispatch({
              type: "getBalanceStores",
              payload: { pageIndex: 1, pageSize, status: "0" }
            });
            break;
          case "/store/registerstore":
              dispatch({
                type: "getStoreRegisters",
                payload: { pageIndex: 1, pageSize }
              });
              break;
        }
      });
    }
  }
};
